import * as CONFIG_URL from './config.js'
const {
  globalData
} = getApp()

const postData = {
  method: 'POST',
  header: {
    "content-type": "application/json"
  }
}

const postFormData = {
  method: 'POST',
  header: {
    "content-type": "application/x-www-form-urlencoded"
  }
}



const getData = {
  method: 'GET',
  header: {
    "content-type": "application/json"
  }
}

// promise  post 统一返回的函数
const promise = (requestData) => {
  return new Promise((resolve, reject) => {
    wx.request(Object.assign(postData, requestData, {
      success: (res) => {
        if (res.data.code == '0') {
          resolve(res.data.data)
        }
      }
    }))
  })
}

// promise  postFormData 统一返回的函数
const postFormPromise = (requestData) => {
  return new Promise((resolve, reject) => {
    wx.request(Object.assign(postFormData, requestData, {
      success: (res) => {
        if (res.data.code == '0') {
          resolve(res.data.data)
        }
      }
    }))
  })
}

// promise  get 统一返回的函数
const getPromise = (requestData) => {
  return new Promise((resolve, reject) => {
    wx.request(Object.assign(getData, requestData, {
      success: (res) => {
        if (res.data.code == '0') {
          resolve(res.data.data)
        }
      }
    }))
  })
}


// 打卡更新到后台函数
const toAttend = (stu_id, date, formal_class_id) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.TO_ATTEND,
    data: {
      stu_id: stu_id,
      formal_class_id: formal_class_id,
      date: date
    }
  }
  return promise(requestData)
}

// 获取经纬度
const getAttence = (campusId, cityId) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.GET_ATTENCE,
    data: {
      campusid: campusId,
      cityid: cityId
    }
  }
  return promise(requestData)
}

// 获取服务器时间
const getTime = () => {
  // console.log(globalData.baseUrl + CONFIG_URL.URL_DATA.GET_TIME)
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.GET_TIME
  }
  return getPromise(requestData)
}

// 获取考勤班级列表 
const myAttendClasses = (stu_id) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.MY_ATTEND_CLASSES,
    data: {
      stuId: stu_id
    }
  }
  return promise(requestData)
}

// 月历查询 ATTEND_MENOLOGY
const attendMenology = (stuId, formalClassId, monthStr) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.ATTEND_MENOLOGY,
    data: {
      stuId: stuId,
      formalClassId: formalClassId,
      monthStr: monthStr
    }
  }
  return promise(requestData)
}

// 更新打卡
const upData = (id, punch_date, status) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.UP_DATA,
    data: {
      id: id,
      punch_date: punch_date,
      status: status
    }
  }
  return new Promise((resolve, reject) => {
    wx.request(Object.assign(postData, requestData, {
      success: (res) => {
        if (res.data.code == '0') {
          resolve(res.data)
        }
      }
    }))
  })
}

// 查找我的班
const findMyPack = (stu_id) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.FIND_MY_CLASS,
    data: {
      stu_id: stu_id
    }
  }
  return postFormPromise(requestData)
}

// 获取个人资料
const WXAboutMe = (cluesOrStuId, type) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.WX_ABOUT_ME,
    data: {
      cluesOrStuId: cluesOrStuId,
      type: type
    }
  }
  return promise(requestData)
}

// 获取课程表
const gettimetable = (classid) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.GET_TIME_TABLE,
    data: {
      "classid": classid
    }
  }
  return promise(requestData)
}

const findIntentNoticeDetail = (noticeId) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.FIND_INTENT_NOTICE_DETAIL,
    data: {
      "noticeId": noticeId
    }
  }
  return promise(requestData)
}
//问卷列表
const getStuQuestionnaire = (stuId) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.FIND_QUEST_BY_STU + stuId
  }
  return getPromise(requestData)
}

//结课问卷列表
const getStuTeacher = (stuId) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.FIND_TEACHER_BY_STU + '/'+stuId
  }
  return getPromise(requestData)
}

//问卷详情
const getQuestionnaireById = (pid,id) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.FIND_QUEST_ID + pid + '/' +id
  }
  return getPromise(requestData)
}
//问卷提交
const submitQues = (id,quesData) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.SUBMIT_QUES,
    data: {
      "id": id,
      "quesData": quesData
    }
  }
  return promise(requestData)
}

// 考核成绩
const getExamResult = (stuId, formalclassid) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.EXAM_RESULTS + '/' + stuId + '/' + formalclassid
  }
  return getPromise(requestData)
}

// 标记所有的消息未读变成已读
const markreadList = (telephone, gmessageType,from) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.ALL_READ,
    data: {
      "telephone": telephone,
      "gmessageType": gmessageType,
      "from": from
    }
  }
  return promise(requestData)
}

// 清除近三个月的消息
const removeThreemonthdataList = (telephone, gmessageType, from) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.REMOVE_NEARLY_THREEMONTH_AGO,
    data: {
      "telephone": telephone,
      "gmessageType": gmessageType,
      "from": from
    }
  }
  return promise(requestData)
}

// 素材文件上传成功，返回数据上传
const saveMaterialList = (stuId, url, key,size,name,type) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.SAVE_MATERIAL,
    data: {
      "stuId": stuId,
      "url": url,
      "key": key,
      "size":size,
      "name":name,
      "type":type
    }
  }
  return promise(requestData)
}

// 获取素材列表
const getMaterialList = (stuId) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.GET_MATERIAL_LIST,
    data: {
      "stuId": stuId
    }
  }
  return promise(requestData)
}

// 删除素材
const deleteAliyunfileList = (id,key) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.DELETE_ALIYUN_FILE,
    data: {
      "id": id,
      "key": key
    }
  }
  return promise(requestData)
}

// 删除素材完执行
const deleteMaterialList = (id) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.DELETE_MATERIAL_FILE,
    data: {
      "id": id
    }
  }
  return promise(requestData)
}

// 获取结课证书海报内容
const getFinishCourse = (hcid) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.GET_FINISH_COURSE,
    data: {
      "hcId": hcid
    }
  }
  return promise(requestData)
}

// 获取海报背景
const getAllPoster = (type,from) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.GET_ALL_POSTER,
    data: {
      "type": type,
      "from":1
    }
  }
  return promise(requestData)
}

//获取打卡海报内容
const getAttendPoster = (id) => {
  let requestData = {
    url: globalData.baseUrl + CONFIG_URL.URL_DATA.GET_ATTEND_POSTER,
    data: {
      "id": id
    }
  }
  return promise(requestData)
}

module.exports = {
  toAttend: toAttend,
  getAttence: getAttence,
  getTime: getTime,
  myAttendClasses: myAttendClasses,
  attendMenology: attendMenology,
  upData: upData,
  findMyPack: findMyPack,
  WXAboutMe: WXAboutMe,
  getTimeTable: gettimetable,
  findIntentNoticeDetail: findIntentNoticeDetail,
  getStuQuestionnaire: getStuQuestionnaire,
  getStuTeacher: getStuTeacher,
  getQuestionnaireById: getQuestionnaireById,
  submitQues: submitQues,
  getExamResult: getExamResult,
  markreadList: markreadList,
  removeThreemonthdataList: removeThreemonthdataList,
  saveMaterialList: saveMaterialList,
  getMaterialList: getMaterialList,
  deleteAliyunfileList: deleteAliyunfileList,
  deleteMaterialList: deleteMaterialList,
  getFinishCourse: getFinishCourse,
  getAllPoster: getAllPoster,
  getAttendPoster: getAttendPoster
}